<?php

namespace app\admin\controller;

use think\Db;
use think\Controller;
use think\Request;
use app\common\model\PermissionModel;
use app\common\model\RoleModel;

class PermissionController extends Controller
{
    public function index()
    {
        return view();
    }

    /**
     * 权限列表
     */
    public function list()
    {
        if ($this->request->isAjax()) {
            $permission = new PermissionModel();
            $result['data'] = $permission->select();
            return pagejson($result['data'], 3, 'ok');
        }
    }

    /**
     * 添加
     */
    public function add()
    {
        if (!request()->isAjax()) {
            $data['permission'] = null;
            $data['permissionsList'] = [];
            return view('form',$data);
        }
        $permission = new PermissionModel();
        if (request()->post('type') == 0) {
            $permission->level = 1;
        } else if (request()->post('type') == 1) {
            $permission->level = 2;
        } else if (request()->post('type') == 2) {
            $permission->level = 3;
        }
        $permission->allowField(true)->save(request()->post());
        return zbcjson();
    }
    /**
     * 修改
     */
    public function edit($id = 0){
        if (!request()->isAjax()) {
            $data['permission'] = PermissionModel::findOrEmpty($id);
            $data['permissionsList'] = PermissionModel::where('level',$data['permission']['level'] - 1);
            return view('form', $data);
        }
         //数据处理
         $user = PermissionModel::find(request()->post('id'));
         $user->allowField(true)->save(request()->post(), ['id' => request()->post('id')]);
         return zbcjson();
    }
    /**
     * 删除
     */
    public function delete($id = null)
    {
        $result  = false;
        //批量删除不是事务的
        if (request()->has('list', 'post')) {
            $result = PermissionModel::destroy(request()->post('list'));
        }
        if (!empty($id)) {
            $result = PermissionModel::destroy($id);
        }
        if ($result) {
            return zbcjson(true);
        }
        return zbcjson(false, '删除错误');
    }
    /**
     * 角色下面得权限列表
     */
    public function permissionsByRoleId($id)
    {
        if (request()->isAjax()) {

            $permissions = (RoleModel::get($id))->permissions->toArray();
            $tree = $this->buildtree($permissions, 0);
            return zbcjson($tree);
        }
    }

    /**
     * 根据级别查询权限
     */
    public function permissionsByLevel($level)
    {
        if (request()->isAjax()) {
            $list = PermissionModel::where('level', $level)->select();
            return zbcjson($list);
        }
    }

    public function buildTree($list)
    {
        $data = [];
        foreach ($list as $key => $value) {
            if ($value['pid'] == 0) {
                unset($list[$key]);
                array_push($data, array(
                    "title" => $value['name'],
                    "id" => $value['id'],
                    "children" => $this->buildNodes($list, $value['id']),
                    "spread" => true
                ));
            }
        }
        return $data;
    }

    public function buildNodes($list, $id)
    {
        if (count($list) == 0)
            return;
        $data = [];
        foreach ($list as $key => $value) {
            if ($value['pid'] == $id) {
                unset($list[$key]);
                array_push($data, array(
                    "title" => $value['name'],
                    "id" => $value['id'],
                    "children" => $this->buildNodes($list, $value['id']),
                    "spread" => true
                ));
            }
        }
        return $data;
    }
}
